/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.util.osgi;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

import org.osgi.framework.Bundle;

/**
 * BundleClassLoader : The OSGi bundle classloader.
 * 
 * <p>
 * This class adapt a {@link Bundle} object into a {@link ClassLoader}. This
 * class is usefull for a soft adaptation of classloader-based mechanisms
 * (serialization, localization, etc.) in an OSGi environment.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class BundleClassLoader extends ClassLoader {

    /** The bundle */
    private final Bundle mBundle;

    /**
     * Constructor
     * 
     * @param pBundle
     *            The bundle
     */
    public BundleClassLoader(final Bundle pBundle) {
        super(Object.class.getClassLoader());

        mBundle = pBundle;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.ClassLoader#getResources(java.lang.String)
     */
    @Override
    public Enumeration<URL> getResources(final String pName) throws IOException {
        return mBundle.getResources(pName);
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.ClassLoader#getResource(java.lang.String)
     */
    @Override
    public URL getResource(final String pName) {
        return mBundle.getResource(pName);
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.ClassLoader#loadClass(java.lang.String)
     */
    @Override
    public Class<?> loadClass(final String pName) throws ClassNotFoundException {
        return mBundle.loadClass(pName);
    }
}
